package com.hebi.xzspring.initailizer;

import com.hebi.xzspring.start.ClazzConfig;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletRegistration;

public class XServletContainerInitializer implements WebApplicationInitializer {

	@Override
	public void onStartup(ServletContext servletContext)  {

		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		context.register(ClazzConfig.class);
		// @EnableWebMvc 导致的 引入这个类DelegatingWebMvcConfiguration Caused by: java.lang.IllegalStateException: No ServletContext set
		context.setServletContext(servletContext);
//		// refresh 对spring容器的初始化
		context.refresh();

		// 对web容器进行初始化
		DispatcherServlet servlet = new DispatcherServlet(context);
		ServletRegistration.Dynamic registion = servletContext.addServlet("dispatcherServlet", servlet);
		if (registion != null){
			// loadOnStartup 在tomcat启动的时候 会调用 HttpservletBean的 init() ->
			registion.setLoadOnStartup(1);
			registion.addMapping("*.do");
		}

	}
}
